package com.imu.wys.controller;


//import com.imu.wys.model.User;
//import com.imu.wys.service.UserService;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.imu.wys.model.User;
import com.imu.wys.service.UserService;
import com.imu.wys.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = {"login"})
    public void login(HttpServletRequest request, HttpServletResponse response, String username, String password) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> map = new HashMap<>();
        User user = userService.findByUserName(username);

        if (null != user) {
            if (user.getPassword().equals(password)) {
//                request.getSession().setAttribute("existUser", user.getUsername());
                StpUtil.login(user.getUsername());
                StpUtil.checkLogin();
                String tocken = StpUtil.getTokenValue();
                map.put("code",200);
                map.put("msg","登录成功");
                map.put("tocken",tocken);
            } else {
                userService.insertUserLog(user,"登录失败");
                map.put("code",201);
                map.put("msg","账户和密码不正确");
            }
        } else {
            map.put("code",201);
            map.put("msg","用户名不存在");
        }
        JSONObject json = new JSONObject(map);
        response.getWriter().print(json.toString());


    }

    @RequestMapping(value = {"register"})
    public void register(HttpServletRequest request, HttpServletResponse response, String username, String password) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> map = new HashMap<>();
        User user = userService.findByUserName(username);
        if (user == null) {
            User registerUser = new User();
            registerUser.setUsername(username);
            registerUser.setPassword(password);
            try {
                userService.insertUser(registerUser);
                map.put("code",200);
                map.put("msg", "注册成功前往登录！");
            } catch (Exception e) {
                map.put("code",201);
                map.put("msg", "注册失败！");
            }

        } else { //用户名存在
            map.put("code",201);
            map.put("msg", "用户已存在");
        }

        JSONObject json = new JSONObject(map);
        response.getWriter().print(json.toString());
    }

    @RequestMapping(value = {"check"})
    public void check(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        Map<String, Object> map = new HashMap<>();
//        User user = (User) request.getSession().getAttribute("existUser");
//        try {
            String userid = (String) StpUtil.getLoginId();
            System.out.println("id:"+StpUtil.getLoginId());
            if(userid!=null){
                map.put("code",200);
                map.put("user",userid);
            }else{
                map.put("code",201);
                map.put("msg", "未登录");
            }
//
//        }catch (NotLoginException e){
//            map.put("code",202);
//            map.put("msg", "已被他人登录");
//        }
        JSONObject json = new JSONObject(map);
        response.getWriter().print(json.toString());

    }

}
